﻿using System;
using System.Collections.Generic;

namespace Sys.Excel
{
    [Serializable]
    public sealed class ColNode
    {
        private List<ColNode> _leafNodes;
        private IList<ColNode> _childNodes;

        public ColNode()
        {
            this.Width = 30;
        }

        internal int Level_Index { get; set; }

        internal IList<ColNode> LeafNodes
        {
            get
            {
                if (this._leafNodes != null)
                {
                    return this._leafNodes;
                }

                this._leafNodes = new List<ColNode>();

                if (this.ChildColNodes != null && this.ChildColNodes.Count > 0)
                {
                    foreach (ColNode node in this.ChildColNodes)
                    {
                        if (node.ChildColNodes == null || node.ChildColNodes.Count == 0)
                        {
                            this._leafNodes.Add(node);

                            continue;
                        }

                        this._leafNodes.AddRange(node.LeafNodes);
                    }
                }
                else
                {
                    this._leafNodes.Add(this);
                }

                return this._leafNodes;
            }
        }

        /// <summary>
        /// 字段
        /// </summary>
        public string Field { get; set; }

        /// <summary>
        /// 合并列中值相同的单元格用做对比的字段
        /// </summary>
        public string MergeField { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 列宽
        /// </summary>
        public uint Width { get; set; }

        /// <summary>
        /// 格式化
        /// </summary>
        public CellFormatter Formatter { get; set; }

        /// <summary>
        /// 格式化器的名称
        /// </summary>
        public string FormatterName { get; set; }

        /// <summary>
        /// 子级
        /// </summary>
        public IList<ColNode> ChildColNodes
        {
            get
            {
                return this._childNodes;
            }
            set
            {
                this._leafNodes = null;
                this._childNodes = value;
            }
        }
    }
}